package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.FinanceAccountsReceivable;
import com.example.demo.service.FinanceAccountsReceivableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.diboot.core.vo.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.example.demo.entity.FinanceReceipt;
import com.example.demo.dto.FinanceReceiptDTO;
import com.example.demo.vo.FinanceReceiptListVO;
import com.example.demo.vo.FinanceReceiptDetailVO;
import com.example.demo.service.FinanceReceiptService;
import com.example.demo.controller.BaseCustomCrudRestController;
import lombok.extern.slf4j.Slf4j;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * 收款单 相关Controller
 *
 * @author MyName
 * @version 1.0
 * @date 2024-03-04
 * Copyright © MyCompany
 */
@RestController
@RequestMapping("/financeReceipt")
@BindPermission(name = "收款单")
@Slf4j
public class FinanceReceiptController extends BaseCustomCrudRestController<FinanceReceipt> {

    @Autowired
    private FinanceReceiptService financeReceiptService;

  @Autowired
  private FinanceAccountsReceivableService financeAccountsReceivableService;
    /**
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/list")
    public JsonResult<List<FinanceReceiptListVO>> getViewObjectListMapping(FinanceReceiptDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, FinanceReceiptListVO.class);
    }

    /**
     * 根据资源id查询ViewObject
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
        return super.getViewObject(id, FinanceReceiptDetailVO.class);
    }

    /**
     * 创建资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/")
    public JsonResult<?> createEntityMapping(@Valid @RequestBody FinanceReceipt entity) throws Exception {
      BigDecimal total = new BigDecimal(BigInteger.ZERO);

        for(FinanceAccountsReceivable financeAccountsReceivable : entity.getMultipleSelection()) {
          total = total.add(financeAccountsReceivable.getAmount());
          financeAccountsReceivable.setStatus("2");
        }
      financeAccountsReceivableService.updateEntities(entity.getMultipleSelection());

      entity.setAmount(total);
        return super.createEntity(entity);
    }

    /**
     * 根据ID更新资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult<?> updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody FinanceReceipt entity) throws Exception {
        return super.updateEntity(id, entity);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult<?> deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
        return super.deleteEntity(id);
    }
}
